/**
 * \file chunk.c
 * \brief Functions for dealing with the Chunk data structure.
 **/

#include "chunk.h"
#include "mem.h"
#include "misc.h"

Chunk* r_chunk_ensure(Chunk* chunk, size_t size)
{
  if (chunk->alloc_size >= size) return chunk;

  chunk->size = size;
  chunk->alloc_size = size;
  chunk = (Chunk*) r_mem_realloc(chunk, size);
  return chunk;
}

Chunk* r_chunk_ensure_a(Chunk* chunk, size_t size)
{
  if (chunk->alloc_size >= size) return chunk;

  chunk->size = size;
  chunk->alloc_size = r_misc_power2(size);
  chunk = (Chunk*) r_mem_realloc(chunk, chunk->alloc_size);
  return chunk;
}

Chunk* r_chunk_new(size_t size)
{
  Chunk* chunk = (Chunk*) r_mem_malloc(size);
  if (chunk == NULL)
    return NULL;
  chunk->size = size;
  chunk->alloc_size = size;
  return chunk;
}
